package com.clean.cleantotal.compores

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseFragment
import com.clean.cleantotal.bean.DataUtils
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.HomeBean
import com.clean.cleantotal.compores.battery.BatteryActivity
import com.clean.cleantotal.compores.clean.NewCleanScanActivity
import com.clean.cleantotal.compores.device.DeviceActivity
import com.clean.cleantotal.compores.large.LargeActivity
import com.clean.cleantotal.compores.process.ProcessActivity
import com.clean.cleantotal.databinding.FragmentHomeBinding
import com.clean.cleantotal.databinding.HeadHomeLayoutBinding
import com.clean.cleantotal.utils.UseStorageUtil
import com.clean.cleantotal.utils.Utils
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.layoutmanager.HoverGridLayoutManager
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.divider
import com.drake.brv.utils.setup


/**
 * 首页
 */
class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    override fun getViewBinding(
        inflater: LayoutInflater, container: ViewGroup?
    ): FragmentHomeBinding {
        return FragmentHomeBinding.inflate(layoutInflater, container, false)
    }

    private var list = ArrayList<Any>()
    override fun initView() {
        list.add(true)
        list.addAll(DataUtils.getHomeList())

        val layoutManager = HoverGridLayoutManager(requireContext(), 2) // 2 则代表列表一行铺满要求跨度为2
        layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if (position < 0) return 1 // 小于0返回
                // 根据类型设置列表item跨度
                return when (mBinding.recyclerView.bindingAdapter.getItemViewType(position)) {
                    R.layout.item_home_layout -> 1 // 设置指定类型的跨度为1, 假设spanCount为2则代表此类型占据宽度为二分之一
                    else -> 2
                }
            }
        }

        mBinding.recyclerView.layoutManager = layoutManager
        mBinding.recyclerView.divider {
            setDivider(15, true)
            orientation = DividerOrientation.VERTICAL
        }.setup {
            addType<Boolean> { R.layout.head_home_layout }
            addType<HomeBean> { R.layout.item_home_layout }
            onBind {
                when (itemViewType) {
                    R.layout.head_home_layout -> {
                        val head = HeadHomeLayoutBinding.bind(itemView)
                        try {
                            val storageBean = UseStorageUtil.queryWithStorageManager(context)
                            storageBean?.let {
                                val used = storageBean.used
                                val total = storageBean.total
                                head.tvCleanDetail.text = "$used / $total"
                                val usedStr = used.split(" ")
                                val totalStr = total.split(" ")
                                try {
                                    if (usedStr.size > 1 && totalStr.size > 1) {
                                        val result =
                                            (usedStr[0].toDouble()) / totalStr[0].toDouble()
                                        head.tvPer.text = "${(result * 100).toInt()}"
                                    }
                                } catch (e: NumberFormatException) {
                                    e.printStackTrace()
                                }
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                }
            }
            onClick(R.id.ivClean) {
                Utils.requestPr(context) {
                    openActivity(NewCleanScanActivity::class.java)
                }
            }
            onClick(R.id.funLayout) {
                val model = getModel<HomeBean>()
                when (model.funType) {
                    FunType.DEVICE -> {
                        openActivity(DeviceActivity::class.java)
                    }

                    FunType.LARGE -> {
                        Utils.requestPr(context) {
                            openActivity(LargeActivity::class.java)
                        }
                    }

                    FunType.BATTERY -> {
                        openActivity(BatteryActivity::class.java)
                    }

                    FunType.PROCESS -> {
                        openActivity(ProcessActivity::class.java)
                    }

                    else -> {

                    }
                }
            }
        }.models = list
    }

    override fun initData(savedInstanceState: Bundle?) {
    }

}